<?php

Class Lib_CallCenter_ZhonTou_Main
{

    private $url = 'http://crm1.zthltx.com:8003/api'; //请求地址
    private $companyName = '广州法明'; //账号
    private $workerId = '8888'; //账号
    private $passWord = 'admin123'; //密码

    private function getToken(){
        $token = Lib_Redis_String::get("ZhonTou_Token");
        if(empty($token)){
            $params = [
                'companyName'=>$this->companyName,
                'workerId'=>$this->workerId,
                'password'=>md5($this->passWord),
            ];
            $token_res = Lib_CallCenter_ZhonTou_Curl::curlPost($this->url.'/login',$params);
            if($token_res['code'] != 100000){
                return $token_res;
            }
            if(strlen($token_res['data']['expireTime']) == 13){
                Lib_Redis_String::expireAt("ZhonTou_Token",substr($token_res['data']['expireTime']),0,10);
            }else{
                Lib_Redis_String::expireAt($token_res['data']['expireTime']);
            }
            $token = $token_res['data']['token'];
        }
        return $token;


    }


    public function Prequest($action, $params = [], $timeout = 20 )
    {

        $ssid = str_replace('.','',uniqid('',true));
        //初始化实例变量
        $heade = [
            'token' => $this->getToken()
        ];
        //请求地址
        $logs = ' ssid='.$ssid;
        $logs .= ' action='.$action;
        $params['ssid'] = $ssid;
        //请求接口
        $result = Lib_CallCenter_ZhonTou_Curl::curlPost($this->url.$action , $params, $timeout, $heade);
        //初始化返回值
        $res = array('code' => 200, 'message' => 'success', 'data' => array());
        $res['ssid'] = $ssid;
        $logs .= ' request='.json_encode($params);
        $logs .= ' response='.json_encode($result);
        //连接错误
        if ($result['errno'] !== 200) {
            $res['data'] = $params;
            $res['message'] = '连接失败';
            Func_Log::write('callCenter_ZhonTou','WARNING',$logs.' link failure');
            return $res;
        }
        unset($result['errno']);
        if (isset($result['result']['error']) && $result['result']['error'] !== 0){
            $res['code'] = 400;
            $res['message'] = $result['result']['msg'];
            Func_Log::write('callCenter_ZhonTou','WARNING',$logs.' return error');
            return $res;
        }
        $res['message'] = $result['result']['msg'];
        Func_Log::write('callCenter_ZhonTou','success',$logs.' request success');
        $res['data'] = $result['data'];
        return $res;
    }
    /**
     * @param $url
     * @param $params
     * @return mixed
     * get请求
     */
    public function Grequest($action, $params = [], $timeout = 20, $nocheck = 0)
    {
        $ssid = str_replace('.','',uniqid('',true));
        //初始化实例变量
        $token = $this->getToken();
        $header = [
            "Accept: application/json;",
            "token: {$token}"
        ];
        //请求地址
        $logs = ' ssid='.$ssid;
        $logs .= ' action='.$action;
        $params['ssid'] = $ssid;
        //请求接口
        $result = Lib_CallCenter_ZhonTou_Curl::curlGet($this->url.$action , $params, $timeout, $header,$nocheck);
        if($nocheck){
            return $result;
        }
        //初始化返回值
        $res = array('code' => 200, 'message' => 'success', 'data' => array());
        $res['ssid'] = $ssid;
        $logs .= ' request='.json_encode($params);
        $logs .= ' response='.json_encode($result);
        //连接错误
        if ($result['errno'] !== 200) {
            $res['data'] = $params;
            $res['message'] = '连接失败';
            Func_Log::write('callCenter_ZhonTou','WARNING',$logs.' link failure');
            return $res;
        }
        unset($result['errno']);
        if (isset($result['result']['error']) && $result['result']['error'] !== 0){
            $res['code'] = 400;
            $res['message'] = $result['result']['msg'];
            Func_Log::write('callCenter_ZhonTou','WARNING',$logs.' return error');
            return $res;
        }
        $res['message'] = $result['result']['msg'];
        Func_Log::write('callCenter_ZhonTou','success',$logs.' request success');
        $res['data'] = $result['data'];
        return $res;
    }

}

